﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Util;
using CodeStage.AntiCheat.ObscuredTypes;
using System;

public partial class CSVReader {

    /// <summary>
    /// 获得指定行的指定属性名的值(string)
    /// </summary>
    /// <param name="dataIdx"></param>
    /// <param name="typeName"></param>
    /// <returns></returns>
    public string get(int dataIdx, string typeName) {
        string ret = "";
        if (m_dataList.Count > dataIdx) {
            Dictionary<string, string> data = m_dataList[dataIdx];
            data.TryGetValue(typeName, out ret);
        }
        else {
#if UNITY_EDITOR
            debug.log(true, debug.Level.error_3, "data index out of range, fileName is: ", m_fileName, ", dataIdx: ", dataIdx);
#endif
        }

        if (ret == "") {
#if UNITY_EDITOR
            //Debug.LogError("can not found, fileName is: " + m_fileName + ", typeName: " + typeName);
#endif
        }

        return ret;
    }

    /// <summary>
    /// 获得指定行的指定属性名的值（int）
    /// </summary>
    /// <param name="dataIdx">行索引</param>
    /// <param name="typeName">属性名</param>
    /// <returns></returns>
    public int getInt(int dataIdx, string typeName) {
        string strItm = get(dataIdx, typeName);

        return int.Parse(strItm);
    }

    /// <summary>
    /// 获得指定行的指定属性名的值（float）
    /// </summary>
    /// <param name="dataIdx">行索引</param>
    /// <param name="typeName">属性名</param>
    /// <returns></returns>
    public float getFloat(int dataIdx, string typeName) {
        string strItm = get(dataIdx, typeName);

        return float.Parse(strItm);
    }

    /// <summary>
    /// 通过id和列名获取内容
    /// </summary>
    /// <param name="id"></param>
    /// <param name="typeName"></param>
    /// <returns></returns>
    public string getById(string id, string typeName) {
        if (m_keys == null) {
#if UNITY_EDITOR
            debug.log("通过id和列名获取内容", m_fileName + " " + typeName + "   " + id + "  error");
#endif
            return "-1";
        }

        Dictionary<string, string> dic;
        m_datadic.TryGetValue(id, out dic);

        string ret = "-1";
        if (dic != null) {
            dic.TryGetValue(typeName, out ret);
        }
        else {
#if UNITY_EDITOR
            //Debug.Log(m_fileName + " typeName:" + typeName + "   id:" + id + "  not found");
#endif
        }

        if (string.IsNullOrEmpty(ret)) {
            ret = "-1";
        }
        return ret;
    }

    /// <summary>
    /// 根据ID获取多语言版本字符串
    /// </summary>
    /// <param name="id"></param>
    /// <param name="typeName"></param>
    /// <returns></returns>
    public string getMultiLanById(string id, string typeName) {
        if (m_keys == null) {
#if UNITY_EDITOR
            Debug.Log(m_fileName + " " + typeName + "   " + id + "  error");
#endif
            return "-1";
        }

        Dictionary<string, string> dic;
        m_datadic.TryGetValue(id, out dic);

        string ret = "-1";
        if (dic != null) {
            dic.TryGetValue(typeName, out ret);
        }
        else {
#if UNITY_EDITOR
            //Debug.Log(m_fileName + " typeName:" + typeName + "   id:" + id + "  not found");
#endif
        }

        if (string.IsNullOrEmpty(ret)) {
            ret = "-1";
        }
        else {
            //MultiLanguage.Instance().getById(ret, GameDefine.GamelanDir[GameDefine.GAME_LANGUAGE]);
        }

        return ret;
    }

    public string getstringById(string id, string typeName) {
        if (m_keys == null) {
#if UNITY_EDITOR
            Debug.Log(m_fileName + " " + typeName + "   " + id + "  error");
#endif
            return "-1";
        }

        Dictionary<string, string> dic;
        m_datadic.TryGetValue(id, out dic);

        string ret = "-1";
        if (dic != null) {
            dic.TryGetValue(typeName, out ret);
        }
        else {
#if UNITY_EDITOR
            //Debug.Log(m_fileName + " typeName:" + typeName + "   id:" + id + "  not found");
#endif
        }

        if (string.IsNullOrEmpty(ret)) {
            ret = "-1";
        }
        return ret;
    }

    public bool getboolById(string id, string typeName) {
        string strItm = getById(id, typeName);
        bool ret = false;
        try {
            int v = strItm.toInt();
            if (v == 0) {
                ret = false;
            }
            else if (v == 1) {
                ret = true;
            }
        }
        catch {
#if UNITY_EDITOR
            debug.log("格式问题 转化bool错误", m_fileName, id, typeName, ret, strItm);
#endif
        }
        return ret;
    }

    public int getintById(string id, string typeName) {
        string strItm = getById(id, typeName);
        int ret = -1;
        try {
            ret = strItm.toInt();
        }
        catch {
#if UNITY_EDITOR
            debug.log("格式问题 转化Int错误", m_fileName, id, typeName, ret, strItm);
#endif
        }
        return ret;
    }

    public float getfloatById(string id, string typeName) {
        string strItm = getById(id, typeName);

        float ret = -1;
        try {
            ret = strItm.toFloat();
        }
        catch {
            debug.log("格式问题 转化float错误 ", m_fileName, id, typeName, ret);
        }
        return ret;
    }

    public Color getColorById(string id, string typeName) {
        return getById(id, typeName).toColor();
    }

    public Vector3 getVector3ById(string id, string typeName) {
        return getById(id, typeName).toVector3();
    }

    public Vector4 getVector4ById(string id, string typeName) {
        return getById(id, typeName).toVector4();
    }

    public Vector2 getVector2ById(string id, string typeName) {
        return getById(id, typeName).toVector2();
    }

    public string[] getArrayById(string id, string typeName) {
        string strItm = getById(id, typeName);
        if (strItm == "-1") {
            return new string[0];
        }
        return strItm.toSplit();
    }

    //时间 2018/8/8/5
    public DateTime getDateTimeById(string id, string typeName) {
        string strItm = getById(id, typeName);
        if (strItm == "-1") {
            debug.log("配置错误!!!", m_fileName, id, typeName);
            return new DateTime();
        }
        int[] times = strItm.toIntArr();
        if (times.Length == 4) {
            DateTime time = new DateTime(times[0], times[1], times[2], times[3], 0, 0);
            return time;
        }
        else if (times.Length == 3) {
            DateTime time = new DateTime(times[0], times[1], times[2], 0, 0, 0);
            return time;
        }
        else {
            debug.log("配置错误!!!", m_fileName, id, typeName);
        }
        return new DateTime();
    }

    //数组Int 例如: 1|2|3|2
    public CSV.ArrayInt getArrayIntById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.ArrayInt arrayInt;
        if (strItm == "-1") {
            return new CSV.ArrayInt(string.Empty);
        }
        arrayInt = new CSV.ArrayInt(strItm);
        return arrayInt;
    }

    //矩阵Int 例如: 1|2|3/1|2|3/1|2|3
    public CSV.MatrixInt getMatrixIntById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.MatrixInt arrayInt;
        if (strItm == "-1") {
            return new CSV.MatrixInt(string.Empty);
        }
        arrayInt = new CSV.MatrixInt(strItm);
        return arrayInt;
    }

    public CSV.SpaceInt getSpaceIntById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.SpaceInt arrayInt;
        if (strItm == "-1") {
            return new CSV.SpaceInt(string.Empty);
        }
        arrayInt = new CSV.SpaceInt(strItm);
        return arrayInt;
    }

    //数组Float 1|2.3|3.2|2
    public CSV.ArrayFloat getArrayFloatById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.ArrayFloat arrayInt;
        if (strItm == "-1") {
            return new CSV.ArrayFloat(string.Empty);
        }
        arrayInt = new CSV.ArrayFloat(strItm);
        return arrayInt;
    }

    //矩阵Float 1|2.3|3.2|2/1|2.3|3.2|2/1|2.3|3.2|2
    public CSV.MatrixFloat getMatrixFloatById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.MatrixFloat arrayInt;
        if (strItm == "-1") {
            return new CSV.MatrixFloat(string.Empty);
        }
        arrayInt = new CSV.MatrixFloat(strItm);
        return arrayInt;
    }

    //矩阵Float 1|2.3|3.2|2/1|2.3|3.2|2/1|2.3|3.2|2
    public CSV.SpaceFloat getSpaceFloatById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.SpaceFloat arrayInt;
        if (strItm == "-1") {
            return new CSV.SpaceFloat(string.Empty);
        }
        arrayInt = new CSV.SpaceFloat(strItm);
        return arrayInt;
    }

    //数组字符串 例如: as|sdd|dff
    public CSV.ArrayString getArrayStringById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.ArrayString arrayInt;
        if (strItm == "-1") {
            return new CSV.ArrayString(string.Empty);
        }
        arrayInt = new CSV.ArrayString(strItm);
        return arrayInt;
    }

    //矩阵字符串 例如:as|sdd|dff/as|sdd|dff/as|sdd|dff
    public CSV.MatrixString getMatrixStringById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.MatrixString arrayInt;
        if (strItm == "-1") {
            return new CSV.MatrixString(string.Empty);
        }
        arrayInt = new CSV.MatrixString(strItm);
        return arrayInt;
    }

    //矩阵字符串 例如:as|sdd|dff/as|sdd|dff/as|sdd|dff
    public CSV.SpaceString getSpaceStringById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.SpaceString arrayInt;
        if (strItm == "-1") {
            return new CSV.SpaceString(string.Empty);
        }
        arrayInt = new CSV.SpaceString(strItm);
        return arrayInt;
    }

    //灵活的结构包 例如:as|sdd|sdd|11|0.25|542|asd
    public CSV.Agility getAgilityById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.Agility arrayInt;
        if (strItm == "-1") {
            return new CSV.Agility(string.Empty);
        }
        arrayInt = new CSV.Agility(strItm);
        return arrayInt;
    }

    public CSV.DicIntString getDicIntStringById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.DicIntString arrayInt;
        if (strItm == "-1") {
            return new CSV.DicIntString(string.Empty);
        }
        arrayInt = new CSV.DicIntString(strItm);
        return arrayInt;
    }

    public CSV.DicIntInt getDicIntIntById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.DicIntInt arrayInt;
        if (strItm == "-1") {
            return new CSV.DicIntInt(string.Empty);
        }
        arrayInt = new CSV.DicIntInt(strItm);
        return arrayInt;
    }

    public CSV.DicIntFloat getDicIntFloatById(string id, string typeName) {
        string strItm = getById(id, typeName);
        CSV.DicIntFloat arrayInt;
        if (strItm == "-1") {
            return new CSV.DicIntFloat(string.Empty);
        }
        arrayInt = new CSV.DicIntFloat(strItm);
        return arrayInt;
    }

}

namespace CSV {
    public struct DicIntString {
        public Dictionary<int, string> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public string this[int index] {
            get {
                string v = "-1";
                if (!vs.TryGetValue(index, out v)) {
                    v = "-1";
                }
                return v;
            }
        }

        public DicIntString(string s) {
            if (s == string.Empty)
                vs = new Dictionary<int, string>();
            else
                vs = s.toDictIntString();
        }

        public Dictionary<int, string> toDic() {
            return vs;
        }
    }

    public struct DicIntInt {
        public Dictionary<int, int> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public int this[int index] {
            get {
                int v = -1;
                if (!vs.TryGetValue(index, out v)) {
                    v = -1;
                }
                return v;
            }
        }

        public DicIntInt(string s) {
            if (s == string.Empty)
                vs = new Dictionary<int, int>();
            else
                vs = s.toDictIntInt();
        }

        public Dictionary<int, int> toDic() {
            return vs;
        }
    }

    public struct DicIntFloat {
        public Dictionary<int, float> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public float this[int index] {
            get {
                float v = -1;
                if (!vs.TryGetValue(index, out v)) {
                    v = -1;
                }
                return v;
            }
        }

        public DicIntFloat(string s) {
            if (s == string.Empty)
                vs = new Dictionary<int, float>();
            else
                vs = s.toDictIntFloat();
        }

        public Dictionary<int, float> toDic() {
            return vs;
        }
    }

    //数组字符串 例如: as|sdd|dff
    public struct ArrayString {
        private List<string> vs;

        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public string this[int index] {
            get {
                string v = "-1";
                if (index >= 0 && index < vs.Count && vs != null) {
                    v = vs[index];
                }
                return v;
            }
        }

        public ArrayString(string s) {
            if (s == string.Empty)
                vs = new List<string>();
            else
                vs = s.toStringList();
        }

        public List<string> toList() {
            return vs;
        }
    }

    //矩阵字符串 例如:as|sdd|dff/as|sdd|dff/as|sdd|dff
    public struct MatrixString {
        private List<List<string>> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public string this[int row, int pos] {
            get {
                string v = "-1";
                if (row >= 0 && row < vs.Count && vs != null) {
                    if (pos >= 0 && pos < vs[row].Count && vs[row] != null)
                        v = vs[row][pos];
                }
                return v;
            }
        }

        public MatrixString(string s) {
            if (s == string.Empty)
                vs = new List<List<string>>();
            else
                vs = s.toStringMatrixList();
        }

        public List<List<string>> toList() {
            return vs;
        }
    }

    //空间字符串 例如:as|sdd|dff/as|sdd|dff/as|sdd|dff^as|sdd|dff/as|sdd|dff/as|sdd|dff
    public struct SpaceString {
        private List<List<List<string>>> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public string this[int axis1, int axis2, int axis3] {
            get {
                string v = "-1";
                if (axis1 >= 0 && axis1 < vs.Count && vs != null)
                    if (axis2 >= 0 && axis2 < vs[axis1].Count && vs[axis1] != null)
                        if (axis3 >= 0 && axis3 < vs[axis1][axis2].Count && vs[axis1][axis2] != null)
                            v = vs[axis1][axis2][axis3];
                return v;
            }
        }

        public SpaceString(string s) {
            if (s == string.Empty)
                vs = new List<List<List<string>>>();
            else
                vs = s.toStringSpaceList();
        }

        public List<List<List<string>>> toList() {
            return vs;
        }
    }

    //数组Int 例如: 1|2|3|2
    public struct ArrayInt {
        private List<ObscuredInt> vs;

        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public int this[int index] {
            get {
                int v = -1;
                if (index >= 0 && index < vs.Count && vs != null) {
                    v = vs[index];
                }
                return v;
            }
        }

        public ArrayInt(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<ObscuredInt>();
            else
                vs = s.toIntList();
        }

        public List<int> toList() {
            List<int> newVs = new List<int>();
            for (int i = 0; i < vs.Count; i++) {
                newVs.Add(vs[i]);
            }
            return newVs;
        }
    }

    //矩阵Int 例如: 1|2|3/1|2|3/1|2|3
    public struct MatrixInt {
        private List<List<ObscuredInt>> vs;

        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public int this[int row, int pos] {
            get {
                int v = -1;
                if (row >= 0 && row < vs.Count && vs != null) {
                    if (pos >= 0 && pos < vs[row].Count && vs[row] != null)
                        v = vs[row][pos];
                }
                return v;
            }
        }

        public MatrixInt(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<List<ObscuredInt>>();
            else
                vs = s.toIntMatrixList();
        }

        public List<List<int>> toList() {
            List<List<int>> newVs = new List<List<int>>();
            for (int j = 0; j < vs.Count; j++) {
                List<int> new1 = new List<int>();
                var cur1 = vs[j];
                for (int i = 0; i < cur1.Count; i++) {
                    new1.Add(cur1[i]);
                }
                newVs.Add(new1);
            }
            return newVs;
        }
    }

    //空间Int 例如:as|sdd|dff/as|sdd|dff/as|sdd|dff^as|sdd|dff/as|sdd|dff/as|sdd|dff
    public struct SpaceInt {
        private List<List<List<ObscuredInt>>> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public int this[int axis1, int axis2, int axis3] {
            get {
                int v = -1;
                if (axis1 >= 0 && axis1 < vs.Count && vs != null)
                    if (axis2 >= 0 && axis2 < vs[axis1].Count && vs[axis1] != null)
                        if (axis3 >= 0 && axis3 < vs[axis1][axis2].Count && vs[axis1][axis2] != null)
                            v = vs[axis1][axis2][axis3];
                return v;
            }
        }

        public SpaceInt(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<List<List<ObscuredInt>>>();
            else
                vs = s.toIntSpaceList();
        }

        public List<List<List<int>>> toList() {
            List<List<List<int>>> newVs = new List<List<List<int>>>();
            for (int z = 0; z < vs.Count; z++) {
                List<List<int>> zv = new List<List<int>>();
                var zc = vs[z];
                for (int y = 0; y < zc.Count; y++) {
                    List<int> yv = new List<int>();
                    var yc = zc[y];
                    for (int x = 0; x < yc.Count; x++) {
                        yv.Add(yc[x]);
                    }
                    zv.Add(yv);
                }
                newVs.Add(zv);
            }

            return newVs;
        }
    }

    //数组Float 例如:1|2.3|3.2|2
    public struct ArrayFloat {
        private List<ObscuredFloat> vs;

        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public float this[int index] {
            get {
                float v = -1;
                if (index >= 0 && index < vs.Count && vs != null) {
                    v = vs[index];
                }
                return v;
            }
        }

        public ArrayFloat(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<ObscuredFloat>();
            else
                vs = s.toFloatList();
        }

        public List<float> toList() {
            List<float> newVs = new List<float>();
            for (int i = 0; i < vs.Count; i++) {
                newVs.Add(vs[i]);
            }
            return newVs;
        }
    }

    //矩阵Float 1|2.3|3.2|2/1|2.3|3.2|2/1|2.3|3.2|2
    public struct MatrixFloat {
        private List<List<ObscuredFloat>> vs;

        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public float this[int row, int pos] {
            get {
                float v = -1;
                if (row >= 0 && row < vs.Count && vs != null) {
                    if (pos >= 0 && pos < vs[row].Count && vs[row] != null)
                        v = vs[row][pos];
                }
                return v;
            }
        }

        public MatrixFloat(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<List<ObscuredFloat>>();
            else
                vs = s.toFloatMatrixList();
        }

        public List<List<float>> toList() {
            List<List<float>> newVs = new List<List<float>>();
            for (int j = 0; j < vs.Count; j++) {
                List<float> new1 = new List<float>();
                var cur1 = vs[j];
                for (int i = 0; i < cur1.Count; i++) {
                    new1.Add(cur1[i]);
                }
                newVs.Add(new1);
            }
            return newVs;
        }
    }

    //空间Float 1|2.3|3.2|2/1|2.3|3.2|2/1|2.3|3.2|2
    public struct SpaceFloat {
        private List<List<List<ObscuredFloat>>> vs;
        public int Count { get { if (vs == null) { return 0; } return vs.Count; } }

        public float this[int axis1, int axis2, int axis3] {
            get {
                float v = -1;
                if (axis1 >= 0 && axis1 < vs.Count && vs != null)
                    if (axis2 >= 0 && axis2 < vs[axis1].Count && vs[axis1] != null)
                        if (axis3 >= 0 && axis3 < vs[axis1][axis2].Count && vs[axis1][axis2] != null)
                            v = vs[axis1][axis2][axis3];
                return v;
            }
        }

        public SpaceFloat(ObscuredString s) {
            if (s == string.Empty)
                vs = new List<List<List<ObscuredFloat>>>();
            else
                vs = s.toFloatSpaceList();
        }

        public List<List<List<float>>> toList() {
            List<List<List<float>>> newVs = new List<List<List<float>>>();
            for (int z = 0; z < vs.Count; z++) {
                List<List<float>> zv = new List<List<float>>();
                var zc = vs[z];
                for (int y = 0; y < zc.Count; y++) {
                    List<float> yv = new List<float>();
                    var yc = zc[y];
                    for (int x = 0; x < yc.Count; x++) {
                        yv.Add(yc[x]);
                    }
                    zv.Add(yv);
                }
                newVs.Add(zv);
            }
            return newVs;
        }
    }

    //灵活的结构包 例如: as|sdd|sdd|11|0.25|542|asd
    public struct Agility {
        private Dictionary<int, int> dicInt;
        private Dictionary<int, float> dicFloat;
        private Dictionary<int, string> dicString;

        public int this[int id, int type] { get { return GetInt(id); } }

        public float this[int id, float type] { get { return GetFloat(id); } }

        public string this[int id, string type] { get { return GetString(id); } }

        public int GetInt(int id) {
            int i = -1; if (dicInt.ContainsKey(id)) { i = dicInt[id]; }
            return i;
        }

        public float GetFloat(int id) {
            float i = -1; if (dicInt.ContainsKey(id)) { i = dicFloat[id]; }
            return i;
        }

        public string GetString(int id) {
            string i = "-1"; if (dicInt.ContainsKey(id)) { i = dicString[id]; }
            return i;
        }

        public Agility(string s) {
            dicInt = new Dictionary<int, int>();
            dicFloat = new Dictionary<int, float>();
            dicString = new Dictionary<int, string>();

            string[] vs = s.toSplit();
            for (int i = 0; i < vs.Length; i++) {
                switch (vs[i].toType()) {
                    case bType.Int:
                        dicInt.Add(i, vs[i].toInt());
                        break;

                    case bType.Float:
                        dicFloat.Add(i, vs[i].toFloat());
                        break;

                    case bType.String:
                        dicString.Add(i, vs[i]);
                        break;
                }
            }
        }
    }
}